package com.market.dao.jpa;

import java.util.List;

import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.market.dao.RoleDao;
import com.market.dao.UserDao;
import com.market.domain.AccountRecord;
import com.market.domain.Message;
import com.market.domain.Order;
import com.market.domain.Role;
import com.market.domain.User;
import com.market.domain.UserActivityRecord;

@Repository("userDao")
public class UserDaoJpa extends GenericDaoJpa<User, Long> implements UserDao {

	@Autowired
	private RoleDao roleDao;
	
	public UserDaoJpa() {
		super(User.class);
	}

	
	
	/**
	 * New user need to generate activated code;
	 */
	@Override
	public User save(User object) {
		if(object.getIsNew()){
			object.setActivateCode(object.generateActivatedCode());
			Role userRole = this.roleDao.findById(1L);   //add role_user to new user
			object.getRoles().add(userRole);
		}
		return super.save(object);
	}

	public void remove(User user) {
		if(this.findById(user.getId()) == null){
			log.debug(this.getClass() + " User with id " + user.getId() + " is not found, can not be deleted");
		}else{
			log.debug(this.getClass() + " User with id " + user.getId() + " is found, will be deleted");
			super.removeById(user.getId());
		}		
	}

	public List<Role> findRoles(User user) {
		String sql = "select role.* from role ,user_role  where user_role.roles_id=role.id and user_role.users_id = "
				+ user.getId();
		Query query = this.em.createNativeQuery(sql, Role.class);
		List<Role> roles = query.getResultList();
		return roles;
	}
	
	

	public List<Message> findMessages(User user) {
		String sql = "select message.* from message, message_user where message.id=message_user.messages_id "
				+ " and message_user.users_id=" + user.getId();
		Query query = this.em.createNativeQuery(sql, Message.class);
		List<Message> messages = query.getResultList();
		return messages;
	}	

	public List<Order> findOrders(User user) {
		String sql = "select orders.* from orders where orders.user_id= " + user.getId();
		Query query = this.em.createNativeQuery(sql, Order.class);
		List<Order> orders = query.getResultList();
		return orders;
	}
	
	public List<AccountRecord> findAccountRecords(User user) {
		String sql = "select accountrecord.* from accountrecord, user_accountrecord  where " 
				+"user_accountrecord.accountrecords_id=accountrecord.id and user_accountrecord.user_id = "
				+ user.getId() + " order by accountrecord.recorddate asc";
		Query query = this.em.createNativeQuery(sql, AccountRecord.class);
		List<AccountRecord> records = query.getResultList();
		return records;
	}
	
	
	
	


	public List<UserActivityRecord> findUserActivityRecords(User user) {
		String sql = "select useractivityrecord.* from useractivityrecord, user_useractivityrecord  where " 
				+"user_useractivityrecord.useractivityrecords_id=useractivityrecord.id and user_useractivityrecord.user_id = "
				+ user.getId() + " order by useractivityrecord.recorddate asc";
		Query query = this.em.createNativeQuery(sql, UserActivityRecord.class);
		List<UserActivityRecord> records = query.getResultList();
		return records;
	}



	public User findByEmailAndPassword(String email, String password) {
		try {
			String sql = "select user.* from user where email = '" + email + "' and password = '" + password + "'";
			Query query = this.em.createNativeQuery(sql, User.class);
			User user = (User)query.getSingleResult();
			if(user != null){
				user.setRoles(this.findRoles(user));
			}
			return user;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public User findByEmail(String email) {
		try {
			String sql = "select user.* from user where email = '" + email + "'";
			Query query = this.em.createNativeQuery(sql, User.class);
			User user = (User)query.getSingleResult();
//			if(user != null){
//				user.setRoles(this.findRoles(user));
//			}
			return user;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	public User findByTelephone(String telehpone) {
		try {
			String sql = "select user.* from user where telephone = '" + telehpone + "'";
			Query query = this.em.createNativeQuery(sql, User.class);
			User user = (User)query.getSingleResult();
//			if(user != null){
//				user.setRoles(this.findRoles(user));
//			}
			return user;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public User activateAccount(Long id, String activatedCode) {
		User user = super.findById(id);
		if(user != null && user.getActivateCode().equalsIgnoreCase(activatedCode)){
			user.setIsActivated(Boolean.TRUE);
			return super.save(user);
		}
		return null;
		
	}
	
	
}
